package com.activity;

import java.text.ParseException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Selection;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.CheckBox;
import android.widget.EditText;

import com.bean.ConteudoBean;
import com.connection.DatabaseConnection;
import com.dao.ConteudoDao;
import com.helper.FormatHelper;

public class NovoConteudoActivity extends Activity {

	EditText data;
	EditText conteudo;
	CheckBox aula1;
	CheckBox aula2;
	CheckBox aula3;
	CheckBox aula4;
	CheckBox aula5;
	
	Bundle param;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.novoconteudo);
		
		data = (EditText)findViewById(R.id.dataTxt);
		data.setOnKeyListener(new DateMask());
		
		
	}

	public void salvar(View v) {
		ConteudoBean conteudoBean = new ConteudoBean();

		conteudo = (EditText)findViewById(R.id.conteudoTxt);		
		
		aula1 = (CheckBox)findViewById(R.id.aula1);
		aula2 = (CheckBox)findViewById(R.id.aula2);
		aula3 = (CheckBox)findViewById(R.id.aula3);
		aula4 = (CheckBox)findViewById(R.id.aula4);
		aula5 = (CheckBox)findViewById(R.id.aula5);
				
		if(aula1.isChecked()){
			conteudoBean.setAula(1);
		}else if(aula2.isChecked()){
			conteudoBean.setAula(2);
		}else if(aula3.isChecked()){
			conteudoBean.setAula(3);
		}else if(aula4.isChecked()){
			conteudoBean.setAula(4);
		}else if(aula5.isChecked()){
			conteudoBean.setAula(5);
		}
		
		new FormatHelper();
		try {
			conteudoBean.setData(FormatHelper.parseDate(data.getText().toString()));
		} catch (ParseException e) {
			AlertDialog.Builder alert = new AlertDialog.Builder(NovoConteudoActivity.this);
			alert.setTitle("Erro!");
			alert.setMessage("A data tem que ter esse formato dd/MM/YYYY");
			alert.setNeutralButton("OK", null);
			alert.show();
		}
		
		conteudoBean.setConteudo(conteudo.getText().toString());
		
		Intent it = getIntent();
		if(it != null){
			param = it.getExtras();
			if(param != null){
				conteudoBean.getUnidade().setId(param.getInt("id_unidade"));
				conteudoBean.getDisciplina().setId(param.getInt("id_disciplina"));
				conteudoBean.getTurma().setId(param.getInt("id_turma"));
				conteudoBean.getBimestre().setId(param.getInt("id_bimestre"));
			}
		}

		DatabaseConnection data = new DatabaseConnection(
				getApplicationContext());
		SQLiteDatabase escola = data.getWritableDatabase();	
		escola.beginTransaction();
		
		new ConteudoDao().insert(escola, conteudoBean);
		escola.setTransactionSuccessful();
		escola.endTransaction();		
		escola.close();		
		
		Intent it_conteudo = new Intent(getApplicationContext(),
				ConteudoActivity.class);
		it_conteudo.putExtras(param);
		startActivity(it_conteudo);

	}

	class DateMask implements OnKeyListener {

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {

			EditText ed = (EditText) v;
			if (event.getAction() == KeyEvent.ACTION_UP
			&& keyCode != KeyEvent.KEYCODE_DEL) {
				int length = ed.getText().toString().length();
				switch (length) {
				case 2: {
					ed.setTextKeepState(ed.getText() + "/");
					break;
				}
				case 5: {
					ed.setTextKeepState(ed.getText() + "/");
					break;
				}
				default:
					break;
				}
			}
			// Posiciona o cursor no fim
			Selection.setSelection(ed.getText(), ed.getText().toString()
			.length());
			return false;
		}
	}
}
